package edu.gatech.arktos;

import java.util.HashSet;
import java.util.List;

import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.WorksheetEntry;

public class GradesDB {
	private SpreadsheetEntry entry;
	private List<WorksheetEntry> worksheets;
	private HashSet<Student> studentList;
	private SpreadsheetService service;
	
	public enum Pages {
	    Details(0), Data(1), Attendance(2), Grades(3), P1_Teams(4), P2_Teams(5),
	    P3_Teams(6), P1_Grades(7), P2_Grades(8), P3_Grades(9), P1_Contri(10), P2_Contri(11),
	    P3_Contri(12);
	    
	    private final int value;
	    private Pages(int value) {
	        this.value = value;
	    }

	    public int getValue() {
	        return value;
	    }
	}
	
	public GradesDB(SpreadsheetEntry entry, SpreadsheetService service){
		this.entry = entry;
		this.service = service;
		try {
			this.worksheets = entry.getWorksheets();
		} catch (Exception ex){
			System.out.println("Fuck");
		}
		studentList = new HashSet<Student>();
		
		//Populate student list		
		Worksheet details = getWorksheet(Pages.Details);
		Worksheet attendance = getWorksheet(Pages.Attendance);
		System.out.println("START LOOP");
		int counter = 1;
		
		while(!details.GetValue(0, counter).equals("[EMPTY CELL]")){
		//while(counter < 20){
			Student temp = new Student();
			temp.setName(details.GetValue(0, counter));
			temp.setGtid(details.GetValue(1, counter));
			temp.setEmailAddress(details.GetValue(2, counter));
			String tempor = attendance.GetValue(1, counter + 1);
			tempor = tempor.substring(0, tempor.length() - 1);
			temp.setAttendance((int) Double.parseDouble(tempor));
			studentList.add(temp);
			counter++;
		}
		System.out.println();
	}
	
	public Worksheet getWorksheet(Pages p){
		return new Worksheet(worksheets.get(p.getValue()), service);
	}
	
	public int getNumStudents() {
		return studentList.size();
	}

	public int getNumAssignments() {
		int count = 0;
		String cellval = new String("");
		Worksheet m = getWorksheet(Pages.Data);
		
		while(!cellval.equals("[EMPTY CELL]")){
			count++;
			cellval = new String(m.GetValue(4, count + 1));
		}
		return count;
	}

	public int getNumProjects() {
		int count = 0;
		String cellval = new String("");
		Worksheet m = getWorksheet(Pages.Data);
		
		while(!cellval.equals("[EMPTY CELL]")){
			count++;
			cellval = new String(m.GetValue(0, count + 1));
		}
		return count;
	}

	public HashSet<Student> getStudents() {
		return studentList;
	}

	public Student getStudentByName(String string) {
		for (Student s : studentList) {
			System.out.println(s.getName());
			if (s.getName().equals(string)){
				System.out.println("YAY");
				return s;
			}
		}
		System.out.println("NO");
		return null;
	}

	public Student getStudentByID(String string) {
		for (Student s : studentList) {
			System.out.println(s.getName());
			if (s.getGtid().equals(string)){
				System.out.println("YAY");
				return s;
			}
		}
		System.out.println("No");
		return null;
	}

}
